package com.leo;

import javafx.application.Application;
import javafx.beans.value.ChangeListener;
import javafx.beans.value.ObservableValue;
import javafx.scene.Scene;
import javafx.scene.control.TextArea;
import javafx.scene.control.TextFormatter;
import javafx.scene.control.TextFormatter.Change;
import javafx.scene.layout.AnchorPane;
import javafx.stage.Stage;
import javafx.util.StringConverter;

import java.util.function.UnaryOperator;

/**
 * @ClassName TextAreaDemo
 * @Description
 * @Author wangss
 * @date 2020.05.19 23:54
 * @Version 1.0
 */
public class TextAreaDemo extends Application {

    public static void main(String[] args) {
        launch(args);
    }

    @Override
    public void start(Stage primaryStage) throws Exception {
        AnchorPane anchorPane = new AnchorPane();
        anchorPane.setStyle("-fx-background-color: #fff2f2");

        TextArea textArea = new TextArea();
        // 指定初始化得行列
        textArea.setPrefRowCount(3);
        textArea.setPrefColumnCount(3);
        // 是否自动换行
        textArea.setWrapText(false);
        // 文本区域大小
        textArea.setPrefWidth(100d);
        textArea.setPrefHeight(100d);

        // 初始化得时候插入字符串
        textArea.insertText(0, "123");

//        textArea.selectPositionCaret(5);

        anchorPane.getChildren().addAll(textArea);
        Scene scene = new Scene(anchorPane);

        primaryStage.setScene(scene);
        primaryStage.setTitle("菜单");
        primaryStage.setWidth(500d);
        primaryStage.setHeight(500d);
        primaryStage.setResizable(false);
        primaryStage.show();

        textArea.selectedTextProperty().addListener(new ChangeListener<String>() {
            @Override
            public void changed(ObservableValue<? extends String> observable, String oldValue, String newValue) {

            }
        });

        textArea.scrollLeftProperty().addListener(new ChangeListener<Number>() {
            @Override
            public void changed(ObservableValue<? extends Number> observable, Number oldValue, Number newValue) {

            }
        });

        // 给文本域加过滤限制
        textArea.setTextFormatter(new TextFormatter<Object>(new UnaryOperator<Change>() {
            @Override
            public Change apply(Change change) {
                // 获得当前字符串
                String text = change.getText();
                if (text.matches("[a-z]*")) {
                    return change;
                }
                return null;
            }
        }));

        textArea.textProperty().addListener(new ChangeListener<String>() {
            @Override
            public void changed(ObservableValue<? extends String> observable, String oldValue, String newValue) {
                textArea.setTextFormatter(new TextFormatter<String>(new StringConverter<String>() {
                    @Override
                    public String toString(String object) {
                        System.out.println("to = " + object);
                        return object;
                    }

                    @Override
                    public String fromString(String string) {
                        System.out.println("from = " + string);
                        if (string.contains("a")) {
                            String value = string.replace("a", "b");
                            return value;
                        }
                        return string;
                    }
                }));
                textArea.commitValue();
            }
        });
    }
}
